<?php
/**
 * Created by PhpStorm.
 * User: jack.shan
 * Date: 2020/9/15
 * Time: 10:32
 */

namespace app\index\controller;

use app\index\BaseController;
use app\model\GoodsType;
use think\facade\Db;
use think\facade\View;

class Goods extends BaseController
{

    public function index()
    {
        return View::fetch("index");
    }

    //获取商品类型数据
    public function getTypes()
    {
        $goodsTypes = Db::connect("mysql2")->query("select * from goods_type");

        return json($goodsTypes);
    }

    public function addtype()
    {
        $pid = $this->request->param("pid");
        if($this->request->isPost())
        {
            $name = $this->request->param('name');

            $goodsType = new GoodsType();
            $goodsType->name = $name;
            $goodsType->pid = $pid;
            $goodsType->save();

            return "商品类型添加成功";
        }

        View::assign([
            'pid' => $pid
        ]);

        return View::fetch('addtype');
    }

    public function edittype()
    {
        $id = $this->request->param("id");

        $type = GoodsType::find($id);

        $typeData = $type->toArray();

        if($this->request->isPost())
        {
            $name = $this->request->param('name');
            $type->name = $name;
            $type->save();

            return "商品类型编辑成功";
        }


        View::assign([
            'typeData' => $typeData
        ]);

        return View::fetch('edittype');
    }

    //商品添加页面
    public function addGoods()
    {

    }

}